package com.zjx.reggie.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjx.reggie.common.CustomException;
import com.zjx.reggie.domain.Category;
import com.zjx.reggie.domain.Dish;
import com.zjx.reggie.domain.Setmeal;
import com.zjx.reggie.mapper.CategoryMapper;
import com.zjx.reggie.service.CategoryService;
import com.zjx.reggie.service.DishService;
import com.zjx.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    /**
     * 跟据id删除分类，删除之前判定菜品和套餐是否有数量，有就直接抛出异常，不进行删除操作；
     *
     * @param id
     */

    @Override
    public void remove(Long id) {
    //添加查询条件，根据分类id进行查询菜品数据
        LambdaQueryWrapper<Dish> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId,id);
        //如果返回的数据大于0，表示已经关联，不执行删除操作
        int count1 = dishService.count(lambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");//已经关联菜品，抛出一个业务异常
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            throw new CustomException("当前分类下关联了套餐，不能删除");//已经关联套餐，抛出一个业务异常
        }
        //正常删除分类
        super.removeById(id);



    }
}
